package com.ruoyi.consumer.controller

import com.alibaba.csp.sentinel.annotation.SentinelResource
import com.alibaba.csp.sentinel.slots.block.BlockException
import com.ruoyi.common.core.web.controller.BaseController
import com.ruoyi.provider.api.RemoteProviderService
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.beans.factory.annotation.Value
import org.springframework.boot.autoconfigure.web.ServerProperties
import org.springframework.cloud.context.config.annotation.RefreshScope
import org.springframework.context.annotation.ScopedProxyMode
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.RestController
import org.springframework.web.client.RestTemplate
import org.springframework.web.client.getForObject


/**
 *@author YanYu
 *@date 2024/9/22 14:02
 */
@RestController
@RefreshScope(proxyMode = ScopedProxyMode.DEFAULT)
open class ConsumerController : BaseController() {

    @Autowired
    private lateinit var serverProperties: ServerProperties

    @Autowired
    private lateinit var remoteProviderService: RemoteProviderService

    private var restTemplate: RestTemplate = RestTemplate()

    @Value("\${likeyoudo.msg:'没有读取到配置'}")
    private lateinit var msg: String

    @GetMapping("/dm")
    fun demo(): String {
        val providerDemo = remoteProviderService.providerDemo()
        return "这是端口为${serverProperties.port}的consumer-demo服务返回的信息<br />$providerDemo"
    }

    @GetMapping("/demo")
    @SentinelResource("consumerDemo", blockHandler = "consumerDemoBlockHandler", fallback = "consumerDemoFallback")
    open fun consumerDemo(): String {
        // 熔断使用的随机时间延迟
        // val sleepCount: Long = (1+Math.random()*1000).toLong()
        // Thread.sleep(sleepCount)
        return "这是consumer-demo服务返回的信息<br />"
    }

    // 服务流量控制处理，参数最后多一个 BlockException，其余与原函数一致。
    fun consumerDemoBlockHandler(ex: BlockException): String {
        println("consumerDemoBlockHandler异常信息：" + ex.message)
        return "{\"code\":\"500\",\"msg\": \"" + "服务流量控制处理\"}"
    }

    // 服务熔断降级处理，函数签名与原函数一致或加一个 Throwable 类型的参数
    fun consumerDemoFallback(throwable: Throwable): String {
        println("consumerDemoFallback异常信息：" + throwable.message)
        return "{\"code\":\"500\",\"msg\": \"" + "服务熔断降级处理\"}"
    }

    @GetMapping("/demo-rest-template")
    fun consumerDemoRestTemplate(): String {
        val providerDemo = restTemplate.getForObject<String>("http://127.0.0.1:9301/demo")
        return "这是端口为${serverProperties.port}的consumer-demo服务返回的信息<br />$providerDemo"
    }

    @GetMapping("/demo-large")
    fun consumerDemoLarge(): String {
        val providerDemo = remoteProviderService.providerDemoLarge()
        val length = providerDemo.length
        return "这是端口为${serverProperties.port}的consumer-demo服务返回的信息<br />$providerDemo<br/>长度为$length"
    }

    @GetMapping("/config")
    fun config(): String {
        return "这是端口为${serverProperties.port}的consumer-demo服务返回的信息<br />读取到的配置为: ${msg}"
    }
}